package com.mvp.shq.washer.ui.shop.shopAddAddress;

import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.ActionBar;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jaeger.library.StatusBarUtil;
import com.mvp.shq.washer.R;
import com.mvp.shq.washer.base.BaseActivity;
import com.mvp.shq.washer.entity.shop.AddressData;
import com.mvp.shq.washer.entity.shop.address.AreaData;
import com.mvp.shq.washer.entity.shop.address.CityData;
import com.mvp.shq.washer.entity.shop.address.ProvinceData;
import com.mvp.shq.washer.entity.shop.address.UnitData;
import com.mvp.shq.washer.rxbus.RxBus;
import com.mvp.shq.washer.utils.DataDictionary;
import com.mvp.shq.washer.utils.EventCode;
import com.mvp.shq.washer.utils.SpUtil;
import com.mvp.shq.washer.utils.T;
import com.mvp.shq.washer.view.widget.addressPicker.AddressPicker;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ShopAddAddressActivity extends BaseActivity<Presenter, Model> implements ShopAddAddressConstract.View {


    @BindView(R.id.back)
    ImageView back;
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.toolbar_menu)
    TextView toolbarMenu;
    @BindView(R.id.et_name)
    EditText etName;
    @BindView(R.id.et_mobile)
    EditText etMobile;
    @BindView(R.id.tv_address)
    TextView tvAddress;
    @BindView(R.id.img_def)
    ImageView imgDef;
    @BindView(R.id.ll_set_def)
    LinearLayout llSetDef;
    @BindView(R.id.et_address_detail)
    EditText etAddressDetail;
    private boolean isDef = false;//是否默认
    private ProvinceData province;
    private CityData city;
    private AreaData area;
    private UnitData unit;
    private boolean isChooseUnit = false;//是否选选择单位
    private AddressData addressData;
    @Override
    public int getLayoutId() {
        return R.layout.activity_shop_add_address;
    }

    @Override
    public void initView() {
        StatusBarUtil.setColor(this, ContextCompat.getColor(context, R.color.money), 9);
        int type = getIntent().getExtras().getInt("type");
        if (type == DataDictionary.ADDRESS_ADD){
            title.setText("新增收货地址");
        }else if (type == DataDictionary.ADDRESS_UPDATE){
            title.setText("修改收货地址");
            addressData = (AddressData) getIntent().getExtras().getSerializable("address");
            initUI();
        }
        toolbarMenu.setText("保存");

    }

    private void initUI(){
        isChooseUnit = true;
        province = new ProvinceData();
        province.setName(addressData.getProvinceName());
        province.setId(addressData.getProvinceId());
        city = new CityData();
        city.setName(addressData.getCityName());
        city.setId(addressData.getCityId());
        area = new AreaData();
        area.setName(addressData.getAreaName());
        area.setId(addressData.getAreaId());
        unit = new UnitData();
        unit.setName(addressData.getUnitName());
        unit.setId(addressData.getUnitId());
        etName.setText(addressData.getReceiverName());
        etMobile.setText(addressData.getReceiverPhone());
        etAddressDetail.setText(addressData.getDetailAddress());
        String addressDes = province.getName();
        addressDes += "-";
        addressDes += city.getName();
        addressDes += "-";
        addressDes += area.getName();
        addressDes += "-";
        addressDes += unit.getName();
        tvAddress.setText(addressDes);
        if (addressData.getStatus().equals("1")){
            isDef = true;
        }
        if (isDef){
            imgDef.setImageResource(R.mipmap.cart_selected);
        }else {
            imgDef.setImageResource(R.mipmap.cart_pressed);
        }
    }


    @OnClick({R.id.back, R.id.toolbar_menu, R.id.ll_choose_address, R.id.ll_set_def})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.toolbar_menu:
                String receiverName = etName.getText().toString();
                String receiverMobile = etMobile.getText().toString();
                String detailAddress = etAddressDetail.getText().toString();
                if (TextUtils.isEmpty(receiverName)){
                    T.show("请先输入收货人姓名");
                    return;
                }
                if (TextUtils.isEmpty(receiverMobile)){
                    T.show("请先输入收货人联系电话");
                }
                if (TextUtils.isEmpty(detailAddress)){
                    T.show("请输入详细地址");
                    return;
                }
                if(!isChooseUnit){
                    T.show("请先选择所在区域");
                    return;
                }
                if (addressData == null){
                    addressData = new AddressData();
                }

                addressData.setReceiverName(receiverName);
                addressData.setReceiverPhone(receiverMobile);
                addressData.setDetailAddress(detailAddress);
                addressData.setProvinceId(province.getId());
                addressData.setProvinceName(province.getName());
                addressData.setCityId(city.getId());
                addressData.setCityName(city.getName());
                addressData.setAreaId(area.getId());
                addressData.setAreaName(area.getName());
                addressData.setUnitId(unit.getId());
                addressData.setUnitName(unit.getName());
                if (isDef){
                    addressData.setStatus("1");
                }else {
                    addressData.setStatus("0");
                }
                showLoadingDialog(true);
                presenter.addOrUpdateAddress(addressData);
                break;
            case R.id.ll_choose_address:
                WindowManager.LayoutParams lp = getWindow().getAttributes();
                lp.alpha = 0.7f;
                getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
                getWindow().setAttributes(lp);
                List<ProvinceData> dataList = new ArrayList<>();
                AddressPicker picker = new AddressPicker(context,this,dataList);
                picker.setListener(new AddressPicker.OnAddressPickListener() {

                    @Override
                    public void onPick(ProvinceData p, CityData c, AreaData a, UnitData u) {
                        province = p;
                        city = c;
                        area = a;
                        unit = u;
                        isChooseUnit = true;
                        String addressDes = province.getName();
                        addressDes += "-";
                        addressDes += city.getName();
                        addressDes += "-";
                        addressDes += area.getName();
                        addressDes += "-";
                        addressDes += unit.getName();
                        tvAddress.setText(addressDes);
                    }
                });
                picker.showAtLocation(back, Gravity.BOTTOM,0,0);
                break;
            case R.id.ll_set_def:
                isDef = !isDef;
                if (isDef){
                    imgDef.setImageResource(R.mipmap.cart_selected);
                }else {
                    imgDef.setImageResource(R.mipmap.cart_pressed);
                }
                break;
        }
    }

    @Override
    public void addOrUpdateAddressS() {
        RxBus.get().send(EventCode.SHOP_REFRESH_ADDRESS_LIST);
        finish();
    }
}
